﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IntegratedOnBordDebug
{
    public class objICCardInfo
    {
        private string _RFIDCardNo = "";
        private ushort _InnerNo = 65535;
        private int _cardType = -1;
        private string _PlateNum = "";
        private string _extData = "";
        private int _fromDevice = 0;
        private string _portName = "";
        private string _ipv4 = "";

        /// <summary>
        /// 上传IC卡号的显示控制一体机 IPV4
        /// </summary>
        public string Ipv4
        {
            get { return _ipv4; }
            set { _ipv4 = value; }
        }

        /// <summary>
        /// 串口号（RFID 设备接入的端口号）
        /// </summary>
        public string PortName
        {
            get { return _portName; }
            set { _portName = value; }
        }

        /// <summary>
        /// 0 串口，1 UDP（显示控制一体机）
        /// </summary>
        public int FromDevice
        {
            get { return _fromDevice; }
            set { _fromDevice = value; }
        }

        public string RFIDCardNo
        {
            get { return _RFIDCardNo; }
            set { _RFIDCardNo = value; }
        }

        public ushort InnerNo
        {
            get { return _InnerNo; }
            set { _InnerNo = value; }
        }

        public int CardType
        {
            get { return _cardType; }
            set { _cardType = value; }
        }

        public string PlateNum
        {
            get { return _PlateNum; }
            set { _PlateNum = value; }
        }

        public string ExtData
        {
            get { return _extData; }
            set { _extData = value; }
        }

        private string BytesToHex(byte[] PlanBytes)
        {
            string hexStr = "";
            if (PlanBytes != null)
            {
                foreach (var item in PlanBytes)
                {
                    hexStr += item.ToString("x2").ToUpper();
                }
            }

            return hexStr;
        }

        /// <summary>
        /// 解析IC卡数据
        /// </summary>
        /// <param name="pDatas">原始数据</param>
        /// <param name="preFix">是否带前缀 0xAA 0xBB 兼容串口读卡器</param>
        /// <returns></returns>
        public bool ParseDataToModel(byte[] pDatas, bool preFix = true)
        {
            bool flag = false;
            if (pDatas != null && pDatas.Length >= 20)
            {
                int idx = 0;
                if (preFix)
                {
                    idx = 2;
                }
                RFIDCardNo = BytesToHex(pDatas.Skip(idx).Take(4).ToArray());
                InnerNo = BitConverter.ToUInt16(pDatas, idx + 4);
                CardType = pDatas[idx + 4];
                PlateNum = Encoding.Default.GetString(pDatas.Skip(idx + 7).Take(9).ToArray());
                ExtData = Encoding.Default.GetString(pDatas.Skip(idx + 16).Take(4).ToArray());
                flag = true;
            }
            return flag;
        }
    }
}
